﻿using System;
using System.IO;

namespace Progream07
{
    class Program
    {
        /// <summary>
        /// 利用FileSteam实现文件的复制
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            var source = "1.txt";
            var target = "2.txt";
            CopyFile(source, target);
            Console.WriteLine("Copy Successed");
        }
        //文件流的复制
        public static void CopyFile(string source,string target)
        {
            //先读取文件流
            using (FileStream fsRead = new FileStream(source, FileMode.OpenOrCreate, FileAccess.Read))
            {
             //写入新的文件流
             using(FileStream fswrite=new FileStream(target,FileMode.OpenOrCreate,FileAccess.Write))
                {
                    //读写文件的缓冲区
                    byte[] buffer = new byte[1024 * 1024 * 10];
                    //用循环读取文件流
                    while(true)
                    {
                        //返回本次读取的实际字节数
                        int r = fsRead.Read(buffer, 0, buffer.Length);
                        //如果读取到0，说明读取完成，跳出循环
                        if (r == 0)
                            break;
                        //写入文件
                        fswrite.Write(buffer, 0, r);
                    }
                }
            }
           
        }
    }
}
